package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.invite.Invite;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendEmployeeMapper;
import com.burgeon.backend.SHOPBACKEND.rest.invite.*;
import com.burgeon.backend.SHOPBACKEND.rest.invite.util.FirstInviteView;
import com.burgeon.backend.SHOPBACKEND.service.BackendInviteService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class BackendInviteServiceImpl implements BackendInviteService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private BackendEmployeeMapper backendEmployeeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public HashMap RVISITTOT(String type, String account) {
        Info info = GetInfo(account);

        FirstInviteView firstInviteView = new FirstInviteView(info.getUrl(), info.getAccount(), info.getPassword());
        QuerryinviteViewData querryinviteViewData = new QuerryinviteViewData(info.getUrl(), info.getAccount(), info.getPassword());

        try {
            firstInviteView.getData();

            return querryinviteViewData.getData(type);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void delExpressionsData(String id, String account) throws ErrorDataException {
        Info info = GetInfo(account);
        DeleteExpressionsData deleteExpressionsData = new DeleteExpressionsData(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            deleteExpressionsData.getData(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }
    }

    @Override
    public void upExpressionsData(String id, String type, String value, String account) throws ErrorDataException {

        Info info = GetInfo(account);
        UpExpressionsData upExpressionsData = new UpExpressionsData(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            upExpressionsData.getData(id,type,value);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }
    }

    @Override
    public void addExpressionsData(String type, String value, String account) throws ErrorDataException {
        Info info = GetInfo(account);
        AddExpressionsData addExpressionsData = new AddExpressionsData(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            addExpressionsData.getData(type,value);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }
    }

    @Override
    public HashMap expressionsData( String type, String account) throws ErrorDataException{
        Info info = GetInfo(account);
        QuerryExpressionsData querryExpressionsData = new QuerryExpressionsData(info.getUrl(), info.getAccount(), info.getPassword());

        try {
            return querryExpressionsData.getData(type);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }
    }

    @Override
    public HashMap getDataByType(String type, Integer start, String account)  throws ErrorDataException {
        Info info = GetInfo(account);
        QuerryInviteData querryInviteData = new QuerryInviteData(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            HashMap data = querryInviteData.getData(type, start);
            if (null == data.get("list")){
                HashMap map = new HashMap();
                map.put("list",null);
                map.put("count",0);
                return map;
            }
            ArrayList<Invite> list =(ArrayList<Invite>) data.get("list");
            for (Invite invite: list){
                invite.setEmpImg(backendEmployeeMapper.findData(invite.getEmpId(),account));
                User user = userMapper.FindUserDataByPhone(invite.getVipPhone(), account);
                if (null!= user){
                    invite.setVipImg(user.getImg());
                }

            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }


    }

    /**
     *
     * @param field A 会员卡号 B 会员名 C 导购名 D 店铺 E 类型:1:生日邀约,2:休眠邀约,
     *              3:活动邀约,4:储值邀约,5:新品邀约, F 日期 G反馈类型
     *
     * @param message
     * @param type
     * @param start
     * @param account
     * @return
     * @throws ErrorDataException
     */
    @Override
    public HashMap FindData(String field, String message, String type, Integer start, String account) throws ErrorDataException {
        HashMap<String,String> condition = new HashMap<String,String>();
        condition.put("A","'C_VIP_ID;CARDNO'");
        condition.put("B","'C_VIP_ID;VIPNAME'");
        condition.put("C","'HR_EMPLOYEE_ID;NAME'");
        condition.put("D","'C_STORE_ID;NAME'");
        condition.put("E","BILLTYPE");
        condition.put("F","BILLDATE");
        condition.put("G","FEEDBACK");

        Info info = GetInfo(account);
        FindnviteData findnviteData = new FindnviteData(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            HashMap data = findnviteData.getData(type,start,condition.get(field),message);
            if (null == data.get("list")){
                HashMap map = new HashMap();
                map.put("list",null);
                map.put("count",0);
                return map;
            }
            ArrayList<Invite> list =(ArrayList<Invite>) data.get("list");
            for (Invite invite: list){
                invite.setEmpImg(backendEmployeeMapper.findData(invite.getEmpId(),account));
                User user = userMapper.FindUserDataByPhone(invite.getVipPhone(), account);
                if (null!= user){
                    invite.setVipImg(user.getImg());
                }
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }

    }

    private Info GetInfo(String account) {
        return accountMapper.GetAccData(account);
    }
}
